package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 郭梓昊
 * @date 2025/7/11 16:38
 * @Description
 */
@Component
public class MyTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0/20 * * * * *")
    public void checkStatus(){
        //定时检查,修改租约状态,租约结束日期<现在日期,则证明已过期,此时则需要修改该租约的状态为已到期
        LambdaUpdateWrapper<LeaseAgreement>leaseAgreementLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        leaseAgreementLambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }
}
